package service.imp;

import dao.CardMapper;
import dao.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import pojo.Card;
import service.CardService;

import java.util.ArrayList;
import java.util.List;

public class CardServicelmpl implements CardService {

    @Override
    public List<Card> getCardList() {
        SqlSession sqlSession = null;
        List<Card> cardList = new ArrayList<>();
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            cardList = sqlSession.getMapper(CardMapper.class).getCardsList();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return null;
    }
    @Override
    public int getCardByidNumber(String idNumber) {
        SqlSession sqlSession = null;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(CardMapper.class).getCardByIdNumber(idNumber);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    @Override
    public int addCard(Card card) {
        SqlSession sqlSession =null;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(CardMapper.class).addCard(card);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    @Override
    public int modify(Integer status, Integer id) {
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(CardMapper.class).modify(status,id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
    @Override
    public int delCard(Integer id) {
        SqlSession sqlSession = null;
        int count = 0;
        try {
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(CardMapper.class).delCard(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
